//
//  ShopCarTableViewCell.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/14.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "ShopCarTableViewCell.h"
@interface ShopCarTableViewCell()<UITextFieldDelegate>

@end

@implementation ShopCarTableViewCell

# pragma mark - Action

- (void)minusBtnClick{
    if (_getjianClick) {
        _getjianClick(self);
    }
}

- (void)plusBtnClick{
    if (_getJiaClick) {
        _getJiaClick(self);
    }
}

#pragma mark-- UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *textStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    [UserModel sharedInstanced].textStr = textStr;
    
    self.carModel.goodCount = [textStr integerValue];
    if (_getTextFieldClick) {
        _getTextFieldClick(self);
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}






# pragma mark - Init
- (id)initWithCellIdentifier:(NSString *)cellID{
    if (self = [super initWithCellIdentifier:cellID]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _piciamgeView = [UIImageView new];
        _piciamgeView.layer.masksToBounds = YES;
        _piciamgeView.image = [UIImage imageNamed:@"shanhaihuilogo"];
        
        
        _goodLabel = [[UILabel alloc] init];
        _goodLabel.font = DEF_FontSize_15;
        _goodLabel.textColor = getColor(@"323232");
        _goodLabel.textAlignment = NSTextAlignmentLeft;
        _goodLabel.text = @"三只松鼠";
        
        
        _pariceLabel = [[UILabel alloc] init];
        _pariceLabel.font = DEF_FontSize_17;
        _pariceLabel.textColor = getColor(@"EC4350");
        _pariceLabel.text = @"￥99.6";
        
        _perLabel = [[UILabel alloc] init];
        _perLabel.font = DEF_FontSize_12;
        _perLabel.textColor = getColor(@"666666");
        _perLabel.text = @"/￥99.6";
        
        _minusBtn = [[UIButton alloc] init];
        [_minusBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
        [_minusBtn addTarget:self action:@selector(minusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _plusBtn = [[UIButton alloc] init];
        [_plusBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _textField = [[UITextField alloc] init];
        _textField.font = DEF_FontSize_14;
        _textField.textColor = getColor(@"323232");
        //_textField.text = @"2";
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.layer.borderWidth = 1.0f;
        _textField.layer.borderColor = [UIColor grayColor].CGColor;
        _textField.delegate = self;
        
        [self sd_addSubviews:@[self.piciamgeView,self.goodLabel,self.pariceLabel,self.perLabel,self.minusBtn,self.plusBtn,self.textField]];
        
        [self layOutFrame];
        
    }
    return self;
}

- (void)layOutFrame{
    ws(self)
    [_piciamgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(UNIT_HEIGHT(15));
        make.left.mas_equalTo(UNIT_WIDTH(15));
        make.height.mas_equalTo(UNIT_WIDTH(75));
        make.width.mas_equalTo(UNIT_HEIGHT(75));
    }];
    
    [_goodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(UNIT_HEIGHT(15));
        make.left.mas_equalTo(weakself.piciamgeView.mas_right).offset(UNIT_WIDTH(21));
        make.right.mas_equalTo(UNIT_WIDTH(-27));
    }];
    
    [_pariceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakself.piciamgeView.mas_right).offset(UNIT_WIDTH(21));
        make.bottom.mas_equalTo(weakself.piciamgeView.mas_bottom);
        make.height.mas_equalTo(UNIT_HEIGHT(14));
    }];
    
    [_perLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakself.pariceLabel.mas_right).offset(0);
        make.top.mas_equalTo(weakself.pariceLabel.mas_top);
        make.bottom.mas_equalTo(weakself.pariceLabel.mas_bottom);
    }];
    
    [_plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(UNIT_WIDTH(-15));
        make.bottom.mas_equalTo(weakself.piciamgeView.mas_bottom);
        make.width.mas_equalTo(UNIT_WIDTH(30));
        make.height.mas_equalTo(UNIT_HEIGHT(30));
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.plusBtn.mas_left).offset(0);
        make.bottom.mas_equalTo(weakself.plusBtn.mas_bottom);
        make.top.mas_equalTo(weakself.plusBtn.mas_top);
        make.width.mas_equalTo(50);
    }];
    
    [_minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.textField.mas_left).offset(0);
        make.bottom.mas_equalTo(weakself.piciamgeView.mas_bottom);
        make.width.mas_equalTo(UNIT_WIDTH(30));
        make.height.mas_equalTo(UNIT_HEIGHT(30));
    }];
}

- (void)setCarModel:(ShopCarModel *)carModel{
    
    _carModel = carModel;
    
    [self.piciamgeView sd_setImageWithURL:[NSURL URLWithString:carModel.picImage]];
    self.goodLabel.text = carModel.name;
    self.perLabel.text = [NSString stringWithFormat:@"/￥%@",carModel.perMoney];
    self.textField.text = carModel.textField;
    self.pariceLabel.text = [NSString stringWithFormat:@"%@",carModel.totalLabel];
    
}


@end
